package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    @Autowired
    private EmpService empService;

    @Override
    public PageResult<Clazz> pageResult(String name, LocalDate begin, LocalDate end, int page, int pageSize) {

        // 1.设置分页参数
        Page<Clazz> pageInfo = Page.of(page, pageSize);
        pageInfo.addOrder(OrderItem.desc("update_time"));

        // 2.设置分页条件并执行查询
        pageInfo = lambdaQuery().like(name != null && !name.isEmpty(), Clazz::getName, name)
                                .between(begin != null && end != null, Clazz::getEndDate, begin, end)
                                .page(pageInfo);

        // 3.封装班主任和状态到员工列表中
        Set<Integer> masterIds = pageInfo.getRecords()
                                         .stream()
                                         .map(Clazz::getMasterId)
                                         .collect(Collectors.toSet());

        if (!masterIds.isEmpty()) {
            List<Emp> empList = empService.listByIds(masterIds);
            Map<Integer, String> empMap = empList.stream()
                                                 .collect(Collectors.toMap(Emp::getId, Emp::getName));

            pageInfo.getRecords()
                    .forEach(clazz -> {
                        clazz.setMasterName(empMap.getOrDefault(clazz.getMasterId(), null));
                    });

            pageInfo.getRecords()
                    .forEach(clazz -> {
                        clazz.setStatus(getClazzStatus(clazz));
                    });
        }

        // 4.封装结果并返回
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
    }

    @Override
    public Clazz getClazzById(Integer id) {
        // 1.获取班级基本信息
        Clazz clazz = getById(id);

        // 2.获取班主任姓名
        String masterName =
                Optional.ofNullable(empService.getOne(new LambdaQueryWrapper<Emp>().eq(Emp::getId,
                                clazz.getMasterId())))
                                    .map(Emp::getName)
                                    .orElse(null);

        clazz.setMasterName(masterName);

        // 3.获取班级状态
        clazz.setStatus(getClazzStatus(clazz));

        // 4.返回结果
        return clazz;
    }


    public String getClazzStatus(Clazz clazz) {
        if (clazz.getEndDate()
                 .isBefore(LocalDate.now()))
        {
            return "已结课";
        } else if (clazz.getBeginDate()
                        .isAfter(LocalDate.now()))
        {
            return "未开班";
        } else {
            return "在读";
        }
    }
}
